package Utils.Gameplay;

import java.util.LinkedHashMap;
import java.util.Map;

import datatypes.Datatypes.tableType;

public class QueryResponse {

	/*
	 * -------------------------------------------------------------------
	 * --------------------------- data members --------------------------
	 * -------------------------------------------------------------------
	 */

	/**
	 * the queryContext in which the response refers to
	 */
	private QueryContext queryContext;

	/**
	 * context id which was received from database
	 */
	private String contextId;
	
	/**
	 * some context information queried from the database
	 * 
	 * TODO: probably should be an array of strings (for more then a single
	 * shred of information)
	 */
	private String contextInfo;

	/**
	 * answer -> answer-result mapping
	 */
	private Map<Answer, Boolean> answers;

	/*
	 * -------------------------------------------------------------------
	 * --------------------------- Constructors --------------------------
	 * -------------------------------------------------------------------
	 */

	public QueryResponse() {
		setAnswers(new LinkedHashMap<Answer, Boolean>());
	}

	public QueryResponse(QueryContext queryContext, String contextInfo) {
		setAnswers(new LinkedHashMap<Answer, Boolean>());
		setQueryContext(queryContext);
		setContextInfo(contextInfo);
	}

	/*
	 * -------------------------------------------------------------------
	 * ------------------------- member functions ------------------------
	 * -------------------------------------------------------------------
	 */

	public Map<Answer, Boolean> getAnswers() {
		return answers;
	}

	public void setAnswers(Map<Answer, Boolean> answers) {
		this.answers = answers;
	}

	public String getContextInfo() {
		return contextInfo;
	}

	public void setContextInfo(String contextInfo) {
		this.contextInfo = contextInfo;
	}

	public void setQueryContext(QueryContext queryContext) {
		this.queryContext = queryContext;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();

		sb.append("context type: " + getContextType().name() + "\n");
		sb.append("result type: " + getResultType().name() + "\n");
		sb.append("context info: " + getContextInfo() + "\n");

		return sb.toString();
	}

	/*
	 * -------------------------------------------------------------------
	 * ----------------------- QueryResponse features ---------------------
	 * -------------------------------------------------------------------
	 */

	/**
	 * get the correct answer
	 */
	public Answer getRightAnswer() {
		boolean validity;
		for (Answer answer : answers.keySet()) {
			validity = answers.get(answer);
			if (validity == true) {
				return answer;
			}
		}

		return null;
	}

	/**
	 * add a new answer to the answer -> answer-result mapping
	 */
	public void addAnswer(Answer answer, boolean validity) {

		// ideally should not be null, but just in case
		if (answers == null) {
			answers = new LinkedHashMap<Answer, Boolean>();
		}

		// add the answer as a new entry
		this.answers.put(answer, validity);
	}

	/**
	 * @return the context type
	 */
	public tableType getContextType() {
		return this.queryContext.getContext();
	}

	/**
	 * @return the result type (i.e connector type in QueryContext terminology)
	 */
	public tableType getResultType() {
		return this.queryContext.getConnector();
	}

	public String getContextId() {
		return contextId;
	}

	public void setContextId(String contextId) {
		this.contextId = contextId;
	}

}
